package com.neutech.maplestory.client;
import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MapleStoryFrame;
import com.neutech.maplestory.util.MyMusicThread;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏的主入口文件
 */
public class MapleStoryClient extends MapleStoryFrame {
    public List<Ground> grounds = new CopyOnWriteArrayList<>();
    public Hero hero=new Hero(this,100,420);//100,415
    public Bar bar = new Bar(this);
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();
    public List<Mob> mobs = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();
    public ItemPackage itemPackage = new ItemPackage(this);
    public List<Item> items = new CopyOnWriteArrayList<>();

    {
        for (int i = 0; i < 7; i++) {
            Mob mob = new Mob(this,800+(i*200),440,5);
            mobs.add(mob);
        } for (int i = 0; i < 3; i++) {
        Mob mob01 = new Mob(this, 900 + (i * 150), 240, 5);
        mobs.add(mob01);
    }

    }
    {

        Ground ground01 = new Ground(this,0,500,470,300);
        grounds.add(ground01);
        Ground ground02 = new Ground(this,470,420,230,350);
        grounds.add(ground02);
        Ground ground03 = new Ground(this,700,500,1600,300);
        grounds.add(ground03);
        Ground ground04 = new Ground(this,750,305,605,45);
        grounds.add(ground04);
        Ground ground05 = new Ground(this,750,304,30,150);
        grounds.add(ground05);
        Ground ground06 = new Ground(this,0,0,"background");
        grounds.add(ground06);

    }
    @Override
    public void init() {
        super.init();
        //添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    //开关切换
                    itemPackage.live =  !itemPackage.live;
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });
        new MyMusicThread("bgm.mp3",true).start();
    }
    @Override
    public void paint(Graphics g) {

        Iterator<Ground> itGrouds = grounds.iterator();
        while (itGrouds.hasNext()){
            Ground ground = itGrouds.next();
            ground.draw(g);
        }
        bar.draw(g);
        hero.draw(g);




        /**
         * 使用迭代器和copyonwritearraylist解决数组索引移动（线程非安全）
         */
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()) {
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);

        }
       Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()) {
            Mob mob = itMobs.next();
            mob.draw(g);
        }
        Iterator<Item> itItems = items.iterator();
        while (itItems.hasNext()) {
           Item item = itItems.next();
            item.draw(g);
        }
        Iterator<Power> itPowers = powers.iterator();
        while ((itPowers.hasNext())){
           Power power= itPowers.next();
           power.draw(g);
        }
        itemPackage.draw(g);


        Font f = g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("mobs.size()："+mobs.size(),100,100);
        g.drawString("hero.HP()："+hero.HP,100,150);
        g.drawString("hero.MP()："+hero.MP,100,200);

        g.setFont(f);

    }

    public static void main(String[] args) {
        new MapleStoryClient().init();
    }
}
